package com.rivulet.foreign.controller;


import com.rivulet.foreign.service.BasicService;
import com.rivulet.foreign.service.ToolService;
import com.rivulet.foreign.tools.Lock;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

import static com.rivulet.foreign.tools.Identity.*;

/**
 * {页面跳转}
 *
 * @author xujiac
 * @create 2021/6/3
 * @since 1.0.0
 */
@Controller
public class BasicController {

    final BasicService service;
    final ToolService authorizationForIdentity;

    public BasicController(BasicService service, ToolService authorizationForIdentity) {
        this.service = service;
        this.authorizationForIdentity = authorizationForIdentity;
    }

    final String home="home";
    final String manager="manager";
    /**
     * loginPage
     */

    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
    public String demo() {
        return "/login";
    }

    @RequestMapping(value = "/rePassword", method = {RequestMethod.GET, RequestMethod.POST})
    public String toPassword() {
        return "/rePassword";
    }

    /**
     * resultsPage
     */
    @RequestMapping(value = "/logout")
    public String logout() {
        return "/results/logout";
    }

    @RequestMapping(value = "/failure")
    public String failure() {
        return "/results/failure";
    }

    /**
     * studentPage
     */
    @RequestMapping(value = "/student/{path}")
    public ModelAndView studentBasicPage(@CookieValue(value = "user_id", defaultValue = "0") String student_id,@PathVariable("path")String path){
        if (!authorizationForIdentity.reAuthorization(student_id, STUDENT)) {
            return new ModelAndView("/results/failure");
        }
        ModelAndView model = new ModelAndView("/student/"+path);
        Map<String, String> map=null;
        if(path.equals(home)){
            map = service.homeShowInformation(Lock.lockOff(student_id));
        }else if(path.equals(manager)){
            map =service.managerShowInformation(Lock.lockOff(student_id));
        } else{
            map=service.studentBasic(Lock.lockOff(student_id));
        }
        model.addAllObjects(map);
        return model;
    }


    /**
     * teacherPage
     */
    @RequestMapping(value = "/teacher/{path}")
    public ModelAndView teacherBasicPage(@CookieValue(value = "user_id") String user_id, @PathVariable("path")String path){
        if (!authorizationForIdentity.reAuthorization(user_id, TEACHER)) {
            return new ModelAndView("/results/failure");
        }
        ModelAndView model = new ModelAndView("/teacher/"+path);
        String id = Lock.lockOff(user_id);
        if(path.equals(home)){
            Map<String, Object> map = service.tHome(id);
            model.addAllObjects(map);
            return model;
        }else {
            Map<String, Object> map = service.teacherBasicShow(id);
            model.addAllObjects(map);
            return model;
        }
    }

    /**
     * rootPage
     */
    @RequestMapping(value = "/root/account")
    public String goAccount(@CookieValue(value = "user_id") String user_id) {
        if (authorizationForIdentity.reAuthorization(user_id, ROOT)) {
            return "/root/root";
        }
        return "/results/failure";
    }
}
